var spider = require('./spider');
var cheerio = require('cheerio');
var pinyin = require("node-pinyin");

function sort(title) {

    title = title.replace(/(^\s*)|(\s*$)/g, "");
    let str = title.substring(0, 1).toUpperCase();

    // 英文字符，数字
    if (str >= 'A' && str <= 'Z') {
        return str;
    } else if (str >= 0 && str <= 9) {
        return '0-9';
    } else {

        var p = pinyin(str, { "style": "normal" })[0][0].toUpperCase();
        console.log(p);
        p = p.substring(0, 1).toUpperCase();
        if ((p >= 'A') && (p <= 'Z')) {
            return p;
        } else {
            return '其他';
        }


    }


}
// setting
var setting = {
    headers: {},
    start: {
        url: 'http://comic.kukudm.com/comictype/3_*PageReplace*.htm',
        // url: 'http://www.1kkk.com/manhua-jpkr-p*PageReplace*/',
        pageItem: 1,
        curPage: 1,
        totalPage: 4000,
        timeout: 2000,
    },
    isFinish: function (_setting) {

        if (_setting.start.curPage > _setting.start.totalPage) {
            console.log('---spider all list---');
            return true;
        } else {
            return false;
        }
    },
    errorHandel: function (err) {
        console.log('-------error catch---------');
        console.log(err);
        console.log(_setting.start.curPage + '/' + _setting.start.totalPage);
    },
    successHandel: function (_setting) {
        console.log('-------success ' + _setting.start.curPage + '/' + _setting.start.totalPage + '---------');
        _setting.start.curPage++;
        spider.start(_setting);
    },
    // 解析
    parse: {
        type: 'html',
        charset: 'gb2312',
        handel: function (res, _setting) {

            // console.log(res);

            var $ = cheerio.load(res);

            var liArry = $('#comicmain dd');

            if (liArry.length == 0) {
                return false;
            }


            var host = "http://comic.kukudm.com";

            var data = [];

            for (var i = 0; i < liArry.length; i++) {
                var item = liArry.eq(i);
                var _title = item.find('a').eq(1).text().replace(/(^\s*)|(\s*$)/g, "");
                data.push({
                    title: _title,
                    href: host + item.find('a').eq(1).attr('href'),
                    img: item.find('a').eq(0).find('img').attr('src'),
                    auth: null,
                    auth_href: null,
                    news: null,
                    news_href: null,
                    isFinish: null,
                    updateTime: item.text().replace(/(^\s*)|(\s*$)/g, ""),
                    type: null,
                    info: null,
                    letter: sort(_title)
                });
            }

            var page = 107;

            // 设置总页数/总个数
            _setting.start.totalPage = page;


            return data;
        }
    },
    mongdb: {
        database: 'manhua',
        collection: 'xinxin',
        type: 'insert',
        insertType: 'many',//many
        query: {}
    }
};

spider.init(setting);


